package cn.tom.basic.controller;

import cn.tom.basic.service.IVerifyCodeService;
import cn.tom.basic.util.AjaxResult;
import cn.tom.basic.util.StrUtils;
import cn.tom.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 接口类：验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IVerifyCodeService verifyCodeService;


    /**
     * 接口：图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        //1.生成图形验证码：StrUtils和hutools - redis的Value
        String imageCode = StrUtils.getComplexRandomString(4);
        //2.将图形验证码保存到redis - 5分钟过期
        redisTemplate.opsForValue().set(key,imageCode,5, TimeUnit.MINUTES);
        //3.将图形验证码写入图片，并编码成base64的字符串响应给前端
        String base64Str = VerifyCodeUtils.verifyCode(115, 40, imageCode);
        return AjaxResult.me().setResultObj(base64Str);
    }


    /**
     * 接口：手机短信验证码
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody Map<String,String> map){
//        verifyCodeService.getPhoneCode(map);
        return new AjaxResult();
    }


    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody Map<String,String> map){
//        verifyCodeService.getBinderSmsCode(map);
        return new AjaxResult();
    }

}
